package com.agrm.order.controller;

import com.agrm.model.common.dtos.ResponseResult;
import com.agrm.model.common.enums.AppHttpCodeEnum;
import com.agrm.model.order.dto.OrderPay;
import com.agrm.model.order.dto.OrderSubmit;
import com.agrm.model.order.pojo.Order;
import com.agrm.model.order.pojo.OrderItem;
import com.agrm.order.service.OrderItemService;
import com.agrm.order.service.OrderService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/order")
public class OrderController {


    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * 根据ID获取订单信息的接口方法
     * 使用GET请求方式，通过id参数查询订单数据
     *
     * @param id 订单ID，通过请求参数传递
     * @return 返回一个ResponseResult对象，包含查询到的订单信息
     */
    @GetMapping("/get")    // 定义GET请求映射，访问路径为/get
    public ResponseResult findById(@RequestParam("id") Integer id) {  // 接收id请求参数
        return ResponseResult.okResult(orderService.getById(id));    // 调用服务层方法查询订单并返回结果
    }
    @GetMapping("/getItemByNo")    // 定义GET请求映射，访问路径为/get
    public ResponseResult getItemByNo(@RequestParam("no") String no) {  // 接收id请求参数
        return ResponseResult.okResult(orderService.getOne(Wrappers.<Order>lambdaQuery().eq(Order::getSeckillNo,no)));    // 调用服务层方法查询订单并返回结果
    }
    @GetMapping("/getByMemberId")
    public ResponseResult getByMemberId(@RequestParam("memberId") Integer memberId){
        List<Order> list = orderService.list(Wrappers.<Order>lambdaQuery().eq(Order::getMemberId, memberId));
        list.forEach(it ->{
            List<OrderItem> itemList = orderItemService.list(Wrappers.<OrderItem>lambdaQuery().eq(OrderItem::getOrderId, it.getId()));
            it.setOrderItems(itemList);
        });
        return ResponseResult.okResult(list);
    }
    /**
     * 提交订单
     * @return
     */
    @PostMapping("/submit")
    public ResponseResult submitOrder(@RequestBody OrderSubmit orderSubmit) {
        if (orderSubmit == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return ResponseResult.okResult(orderService.submitOrder(orderSubmit) == 1);
    }

    @PostMapping("/seckill")
    public ResponseResult seckill(@RequestBody OrderSubmit orderSubmit) {
        if (orderSubmit == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return ResponseResult.okResult(orderService.saveForSeckill(orderSubmit));
    }

    //判断秒杀订单是否已成功创建
    @GetMapping("/check_order_created")
    public ResponseResult checkIfOrderCreated(String seckillNo) {
        Boolean success = (Boolean) this.redisTemplate.opsForValue().get("seckill-order:" + seckillNo);
        if (Boolean.FALSE.equals(success)) {
            return ResponseResult.errorResult(-1,"创建秒杀订单失败");
        }

        Order order = this.orderService.getOne(Wrappers.<Order>lambdaQuery().eq(Order::getSeckillNo,seckillNo));
        if (order != null) {
            return ResponseResult.okResult(order.getId());
        }

        //告知前端，请继续轮询
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"秒杀订单尚未创建！");
    }
    /**
     * 支付订单
     * @return
     */
    @PostMapping("/pay")
    public ResponseResult payOrder(@RequestBody OrderPay orderPay){
        if (orderPay == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        return ResponseResult.okResult(orderService.payOrder(orderPay) == 1);
    }


    /**
     * 取消订单
     * @param orderNo
     * @return
     */
    @DeleteMapping("/cancel")
    public ResponseResult cancelOrder(@RequestParam("orderNo") String orderNo) {
        return ResponseResult.okResult(orderService.cancelOrder(orderNo) == 1);
    }


    /**
     * 删除订单 逻辑删除
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    public ResponseResult delete(@RequestBody List<Integer> ids) {
        return ResponseResult.okResult(orderService.deleteByIds(ids) > 0);
    }
    @DeleteMapping("/deleteItem")
    public ResponseResult deleteItem(@RequestBody List<Integer> ids) {
        return ResponseResult.okResult(orderItemService.removeByIds(ids));
    }

    @PutMapping("/update")
    public ResponseResult update(@RequestBody Order order) {
        return ResponseResult.okResult(orderService.updateById(order));
    }

    @GetMapping("/getItemById")
    public ResponseResult findItemById(@RequestParam("id") Integer id){
        return ResponseResult.okResult(orderItemService.list(Wrappers.<OrderItem>lambdaQuery().eq(OrderItem::getOrderId,id)));
    }
}
